<template>
  <div class="login">
    <router-link to="/">
      <img
        src="https://t7.baidu.com/it/u=1956604245,3662848045&fm=193&f=GIF"
        class="logo"
        alt=""
    /></router-link>
    <van-form @submit="onSubmit">
      <van-field
        v-model="userName"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <router-link :to="{ name: 'Reg' }">没有账号，我要注册</router-link>
  </div>
</template>

<script>
import { Notify } from "vant";
import { setToken } from "../utils/auth";
import { loginAPI } from "../services/auth";
export default {
  data() {
    return {
      userName: "",
      password: "",
    };
  },
  methods: {
    async onSubmit() {
      const info = await loginAPI({
        userName: this.userName,
        password: this.password,
      });
      console.log(info);
      if (info.code == 1) {
        Notify({ type: "success", message: "登陆成功" });
        setToken(info.data);
        this.$router.replace({
          name: "User",
        });
      } else {
        Notify({ type: "warning", message: info.data });
      }

      // .catch(() => {});
    },
  },
};
</script>

<style scoped>
.logo {
  width: 45vw;
  display: block;
  border-radius: 20px;
  margin: 30px auto;
}
</style>
